package org.xa3apm.vote.hibernate.entities;

import java.io.Serializable;
import javax.persistence.*;

import org.xa3apm.vote.hibernate.entities.Person;

/**
 * Entity implementation class for Entity: Customer
 * 
 */
@Entity(name = "Candidate")
@DiscriminatorValue("Candidate")
@NamedQueries({
		@NamedQuery(name = "Candidate.getById", query = "SELECT C FROM Candidate C WHERE C.id = :id"),
		@NamedQuery(name = "Candidate.getAll", query = "SELECT C FROM Candidate C"),
		@NamedQuery(name = "Candidate.getAllByType", query = "SELECT C FROM Candidate C WHERE C.type = :type"),
		@NamedQuery(name = "Candidate.getAllBySettlement", query = "SELECT C FROM Candidate C WHERE C.address.settlements = :settlement"),
		@NamedQuery(name = "Candidate.getAllByMunicipality", query = "SELECT C FROM Candidate C WHERE C.address.settlements.municipality = :municipality") })
public class Candidate extends Person implements Serializable {
	private static final long serialVersionUID = 1L;
	@ManyToOne(targetEntity = Party.class, optional = true)
	private Party party;
	@Enumerated(EnumType.STRING)
	private VoteType type;

	public Candidate() {
		super();
	}

	public Candidate(Party party, VoteType type) {
		super();
		this.party = party;
		this.type = type;
	}

	/**
	 * @return the party
	 */
	public final Party getParty() {
		return party;
	}

	/**
	 * @param party
	 *            the party to set
	 */
	public final void setParty(Party party) {
		this.party = party;
	}

	/**
	 * @return the type
	 */
	public final VoteType getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public final void setType(VoteType type) {
		this.type = type;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((party == null) ? 0 : party.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (!(obj instanceof Candidate))
			return false;
		Candidate other = (Candidate) obj;
		if (party == null) {
			if (other.party != null)
				return false;
		} else if (!party.getId().equals(other.party.getId()))
			return false;
		if (type != other.type)
			return false;
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return this.getId() + this.getfName() + " " + this.getmName() + " "
				+ this.getlName() + " " + this.getAddress() + this.getAccount()
				+ this.getParty() + " " + type + "]";
	}

}